//
//  SettingViewController.m
//  Newproject3
//
//  Created by rimi on 16/9/12.
//  Copyright © 2016年 Liang. All rights reserved.
//

#import "SettingViewController.h"
#import "SettingTableViewCell.h"
#import "LoginViewController.h"
#import "ClearCacheViewController.h"
#import "AboutUsViewController.h"
#import "ShareViewController.h"
#import "ModifyPasswordViewController.h"
#import "NetWorking.h"
#import "HUser.h"

#import <ShareSDK/ShareSDK.h>
#import <ShareSDKConnector/ShareSDKConnector.h>
#import <ShareSDKUI/ShareSDKUI.h>
// QQ
#import <TencentOpenAPI/TencentOAuth.h>
#import <TencentOpenAPI/QQApiInterface.h>
// wechat
#import "wxApi.h"
// sinaweibo
#import "WeiboSDK.h"

@interface SettingViewController ()<UITableViewDataSource,UITableViewDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>{
    NSArray *_arr1;
    NSDictionary *_dict;
}
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong)UIButton *loginButton;
@property (nonatomic,strong)UILabel *label1;
@property (nonatomic,strong) UIButton *lastButton;
@end

@implementation SettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initUi];
    [self initData];

}

-(void)viewWillAppear:(BOOL)animated{
    if ([HUser currentUser].isLogin == YES) {
        
        self.label1.text = [HUser currentUser].name;
        [self.lastButton setTitle:@"退出登录" forState:UIControlStateNormal];
        
    }else{
        self.label1.text = @"请登录";
        [self.lastButton setTitle:@"登录" forState:UIControlStateNormal];
        
    }

}

-(void)initUi{
    [self.view addSubview:self.tableView];
}

-(void)initData{
    _arr1 = @[@"分享推荐",@"修改密码",@"关于我们",@"清除缓存"];
    
    
}

-(void)action_Button:(UIButton *)sender{
    if (sender.tag == 100) {
        
        if ([HUser currentUser].isLogin != YES) {
            LoginViewController *vc = [[LoginViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }
        else{
            //获取相片或者照相
           
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"请选择" preferredStyle:UIAlertControllerStyleActionSheet];
            
            UIAlertAction *photoAction = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                //打开相册
                [self openPhoto];
            }];
            
            UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                // 打开相机
                [self openCamera];
            }];
            
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
            [alertController addAction:photoAction];
            [alertController addAction:cameraAction];
            [alertController addAction:cancelAction];
            
            [self presentViewController:alertController animated:YES completion:nil];
            
        }
        
       
    }
    else{
        
        if ([HUser currentUser].isLogin == YES) {
            //退出登录状态
            [HUser currentUser].isLogin = NO;
            [self.lastButton setTitle:@"登录" forState:UIControlStateNormal];
            self.label1.text = @"请登录";

            
        }else{
            
            static LoginViewController *vc = nil;
            vc = [[LoginViewController alloc]init];
            vc.block = ^(NSDictionary *dict){
                _dict = dict;
            };
            [self.navigationController pushViewController:vc animated:YES];
        }

    }
}

/////////////////////////////////////////
#pragma mark -- UIImagePickerControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    // 相册中选择的图片
    UIImage *image = info[UIImagePickerControllerOriginalImage];
    // 赋值
    //    [self.loginButton setBackgroundImage:image forState:UIControlStateNormal];
    [self.loginButton setImage:image forState:UIControlStateNormal];
    // 选择过后相册消失
    [picker dismissViewControllerAnimated:YES completion:nil];
}

// 不选择然后取消
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [picker dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark -- 相机相册

-(void)openPhoto{
    // 相机相册管理器
    UIImagePickerController *pickerController = [[UIImagePickerController alloc]init];
    // 设置数据控制源类型
    pickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    pickerController.delegate = self;
    // 允许编辑
    pickerController.allowsEditing = YES;
    
    [self presentViewController:pickerController animated:YES completion:nil];
}

-(void)openCamera{
    // 判断是否支持相机
    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        
        UIAlertController *controller = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"相机不可用" preferredStyle:UIAlertControllerStyleActionSheet];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
        [controller addAction:action];
        [self presentViewController:controller animated:YES completion:nil];
        
        return;
    }
    UIImagePickerController *controller = [[UIImagePickerController alloc]init];
    controller.sourceType = UIImagePickerControllerSourceTypeCamera;
    controller.delegate = self;
    controller.allowsEditing = YES;
    
    [self presentViewController:controller animated:YES completion:nil];
}

/////////////////////////////////////////



#pragma mark -- UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return khieght*0.3/5;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
//        ShareViewController *vc = [[ShareViewController alloc]init];
//        [self.navigationController pushViewController:vc animated:YES];
        
        NSArray* imageArray = @[[UIImage imageNamed:@"福包-头像@3x"]];
        if (imageArray) {
            
            NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
            [shareParams SSDKSetupShareParamsByText:@"Share Content"
                                             images:imageArray
                                                url:[NSURL URLWithString:@"http://mob.com"]
                                              title:@"ShareTitle"
                                               type:SSDKContentTypeAuto];
            //2、To show the share content view
            [ShareSDK showShareActionSheet:nil                                      items:nil
                               shareParams:shareParams
                       onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) {
                           
                           switch (state) {
                               case SSDKResponseStateSuccess:
                               {
                                   UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Share Success!"
                                                                                       message:nil
                                                                                      delegate:nil
                                                                             cancelButtonTitle:@"OK"
                                                                             otherButtonTitles:nil];
                                   [alertView show];
                                   break;
                               }
                               case SSDKResponseStateFail:
                               {
                                   UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Share Fail"
                                                                                   message:[NSString stringWithFormat:@"%@",error]
                                                                                  delegate:nil
                                                                         cancelButtonTitle:@"OK"
                                                                         otherButtonTitles:nil, nil];
                                   [alert show];
                                   break;
                               }
                               default:
                                   break;
                           }
                           
                       }];

        }
    }
    if (indexPath.row == 1) {
        ModifyPasswordViewController *vc = [[ModifyPasswordViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }
    if (indexPath.row == 2) {
        AboutUsViewController *vc = [[AboutUsViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }
    if (indexPath.row == 3) {
        ClearCacheViewController *vc = [[ClearCacheViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
        
        
    }
}

#pragma mark -- UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 4;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SettingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.leftImage.image = [UIImage imageNamed:@"icon-我的@2x"];
    cell.messageLabel.text = _arr1[indexPath.row];
    cell.rightImage.image = [UIImage imageNamed:@"个人中心-right@2x"];
    return cell;
}


#pragma mark -- getter

-(UITableView *)tableView{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kwidth, khieght) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        //_tableView.scrollEnabled = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[SettingTableViewCell class] forCellReuseIdentifier:@"cell"];
        UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kwidth, khieght*0.4)];
        UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kwidth, khieght*0.4)];
        image.image = [UIImage imageNamed:@"0.jpg"];
        UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(kwidth*0.3, CGRectGetMaxY(self.label1.frame)+khieght*0.4*0.05, kwidth*0.4, khieght*0.4*0.1)];
        label2.textAlignment = NSTextAlignmentCenter;
        label2.text = @"登陆注册后更精彩";
        [headView addSubview:image];
        [headView addSubview:self.loginButton];
        [headView addSubview:self.label1];
        [headView addSubview:label2];
        _tableView.tableHeaderView = headView;
        
        UIView *footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kwidth, khieght*0.3)];
        [footView addSubview:self.lastButton];
        _tableView.tableFooterView = footView;
        
    }
    return _tableView;
}

-(UILabel *)label1{
    if (_label1 == nil) {
        _label1 = [[UILabel alloc]initWithFrame:CGRectMake(kwidth*0.3, CGRectGetMaxY(self.loginButton.frame)+khieght*0.4*0.05, kwidth*0.4, khieght*0.4*0.1)];
         _label1.textAlignment = NSTextAlignmentCenter;
        _label1.text = @"请注册";
    }
    return _label1;
}

-(UIButton *)loginButton{
    if (_loginButton == nil) {
        _loginButton = [[UIButton alloc]initWithFrame:CGRectMake(kwidth*0.4, khieght*0.1, kwidth*0.2, kwidth*0.2)];
        _loginButton.tag = 100;
        [_loginButton addTarget:self action:@selector(action_Button:) forControlEvents:UIControlEventTouchUpInside];
        [_loginButton setImage:[UIImage imageNamed:@"个人中心-B"] forState:UIControlStateNormal];
    }
    return _loginButton;
}

-(UIButton *)lastButton{
    if (_lastButton == nil) {
        _lastButton = [[UIButton alloc]initWithFrame:CGRectMake(kwidth*0.1, khieght*0.08, kwidth*0.8, khieght*0.08)];
        [_lastButton setBackgroundColor:themeColor];
        [_lastButton setTitle:@"登录" forState:UIControlStateNormal];
        [_lastButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _lastButton.tag = 101;
        [_lastButton addTarget:self action:@selector(action_Button:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _lastButton;
}
@end
